﻿@using Newtouch.Infrastructure

@{
    ViewBag.Title = "排班日历";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>
    body {
        background-color:#fff;
    }
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .main {
        width: 100%;
        height: 730px;
        margin: 30px auto;
    }
    .cxtj {
        width: 96%;
    }
    button {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background: #73d4ff;
        font-size: 12px;
        text-align: center;
        line-height: 30px;
        border: none;
        position: absolute;
        color: #fff;
        outline: none;
        cursor: pointer;
        user-select: none;
    }

    .prev {
        top: 10px;
        left: 35%;
    }
    .xzsj {
        top: 30px;
        left: 1%;
        position:relative;
    }
    .next {
        top: 10px;
        right: 40%;
    }

    .nowTime {
        width: 120px;
        position: absolute;
        top: 15px;
        right: calc(50% - 45px);
        font-size: 20px;
        font-weight: bold;
        background: linear-gradient(to right, rgb(48, 246, 159), #b5e4f8);
        -webkit-background-clip: text;
        color: transparent;
    }

    .container {
        width: 1205px;
        margin: 50px auto;
    }

        .container ul:first-of-type {
            background: linear-gradient(to right, rgb(202, 254, 231), #b5e4f8);
            color: rgb(43, 40, 40);
        }

        .container ul {
            width: 1205px;
            margin: 0 auto;
            display: flex;
            flex-wrap: wrap;
        }

        .container li {
            width: 170px;
            height: 100px;
            text-align: center;
            line-height: 25px;
            cursor: pointer;
            border:1px solid #dbdbdb;
        }

            .container li:not(.disabled):hover {
                border: 1px solid #fe6d6d;
            }

    li.disabled {
        background: linear-gradient(to right, #e8efe6, #e8efe6);
        cursor: not-allowed;
        color: #00608a;
    }

    li.active {
        border: 1px solid rgb(246, 170, 165);
        /*background: linear-gradient(to right, rgb(252, 202, 199), rgb(252, 244, 243));*/
    }
        li.active span {
            font-weight:bold;
            color:red;
        }
    .ultitle li{
        width: 170px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        font-size:20px;
        cursor: pointer;
        border: 1px solid #73d4ff;
    }
    .syhys {
        margin-left: 10px;
        margin-right: 5px;
        width: 90%;
        height: 20px;
        overflow: hidden;
        position: relative;
        box-sizing: border-box;
        background-color: #ffffff;
        border-radius: 15px;
    }
        .syhys:hover {
            border: 1.5px solid #fe6d6d;
            box-shadow: 0 6px 8px 0 rgba(0,0,0,0.24), 0 8px 25px 0 rgba(0,0,0,0.19);
        }
    .rqxs {
        width: 170px;
        height: 20px;
        background-color: #eaeaea;
        font-size:20px;
    }
    .progress-bg {
        height: 100%;
        box-sizing: border-box;
        background: #99ccff;
        animation: panoramic 20s linear infinite;
        border-radius: 5px;
        border: 1px solid #b7e5fa;
    }

    .progress-zj {
        height: 100%;
        box-sizing: border-box;
        background: #fede7e;
        animation: panoramic 20s linear infinite;
        border-radius: 5px;
        border: 1px solid #ffcc33;
    }

    .progress-qt {
        height: 100%;
        box-sizing: border-box;
        background: rgb(202, 254, 231);
        animation: panoramic 20s linear infinite;
        border-radius: 5px;
        border: 1px solid rgb(202, 254, 231);
    }
    .xszdc {
        position: relative;
        font-size: 12px;
        top:-20px;
        border:1px solid #fcf6f6;
        border-radius:5px;
    }
        .xszdc:hover {
            font-size: 15px;
        }
    .ztsz:hover {
        font-size: 15px;
    }
</style>
<div id="cxdiv" class="panel panel-default" style="background-color:#fff">
    <div class="panel-heading navb-bg">
        筛选条件
    </div>
    <table class="form">
        <tr>
            <th class="formTitle">开始日期：</th>
            <td class="formValue">
                <input id="kssj" type="text" class="form-control input-wdatepicker formClearIgnore" style="float:left;" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />            </td>
            <th class="formTitle">结束日期：</th>
            <td class="formValue">
                <input id="jssj" type="text" class="form-control input-wdatepicker formClearIgnore" style="float:left; margin-left:5%;" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
            </td>
            <th class="formTitle">排班医生：</th>
            <td class="formValue"><input type="text" id="doctormc" name="doctormc" class="form-control form-an" /></td>
            <th class="formTitle">出诊状态：</th>
            <td class="formValue">
                <select class="form-control" id="czztcx">
                    <option value="0">-----</option>
                    <option value="1" selected="selected">已出诊</option>
                    <option value="2">已停诊</option>
                </select>
            </td>
            <td class="formTitle">
                <input type="button" id="btn_search" class="btn btn-primary" value="查询" />
            </td>
            <td class="formTitle">
                <input type="button" id="btn_zhrl" class="btn btn-primary" value="转换日历列表" />
            </td>
        </tr>
    </table>
</div>
<div class="main">
    <div class="cxtj">
        <table class="xzsj form">
            <tr>
                <th class="formTitle">时间 ：</th>
                <td class="formValue">
                    <input type="text" id="rlsj" class="form-control input-wdatepicker formClearIgnore" style="height:20px;width:100px" value="@DateTime.Now.ToString("yyyy-MM")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM' });xzsj()" />
                </td>
                <th class="formTitle">科室 ：</th>
                <td class="formValue">
                    <input type="hidden" class="form-control" id="ks" />
                    <input type="text" class="form-control required" id="ksmz" />
                </td>
                <th class="formTitle">门诊类型：</th>
                <td class="formValue">
                    <select id="mjzbz" class="form-control required" data-EnumType="EnumOutPatientType">
                        <option value="">请选择</option>
                    </select>
                    @*<select id="mjzbz" name="mjzbz" class="form-control required">
            <option value="">==请选择==</option>
            <option value="1">普通门诊</option>
            <option value="2">急诊</option>
            <option value="3">专家门诊</option>
            <option value="4">特病门诊</option>
            <option value="5">重大疾病门诊</option>
            <option value="6">慢性病门诊</option>
            <option value="7">居民两病</option>
            <option value="8">意外伤害门诊</option>
            <option value="9">生育门诊</option>
            <option value="10">耐多药结核门诊</option>
            <option value="11">儿童两病门诊</option>
        </select>*@
                </td>
                <th class="formTitle">排班医生：</th>
                <td class="formValue"><input type="text" id="doctormc2" name="doctormc2" class="form-control form-an" /></td>
                <td class="formTitle">
                    <input type="button" style="float:right" id="btn_searchrl" class="btn btn-primary" value="查询" />
                </td>
            </tr>
        </table>
        </div>
        <button class="prev"><span class="glyphicon glyphicon-chevron-left" title="上个月"></span></button>
        <span class="nowTime">
        </span>
        <button class="next"><span class="glyphicon glyphicon-chevron-right" title="下个月"></span></button>
        <div class="container">
            <ul class="ultitle">
                <li>周日</li>
                <li>周一</li>
                <li>周二</li>
                <li>周三</li>
                <li>周四</li>
                <li>周五</li>
                <li>周六</li>
            </ul>
            <ul class="content"></ul>
        </div>
    </div>
    <div class="gridPanel">
        <table id="gridList"></table>
        <div id="gridPager"></div>
    </div>

    @Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
    {
        ShowKeyList = new int[] {4,8},
        F4Text = "转换排班列表",
        F8Text = "保存"
    })
    <script>
        var isf = true;
        function formatDate(datetime) {
            var month = (datetime.getMonth() < 9) ? '0' + (datetime.getMonth() + 1) : (datetime.getMonth() + 1);
            var thatDate = datetime.getFullYear() + "-" + month;
            // 返回
            return thatDate;
        }
        function loadrl(gridzljyData,loaddata) {
            let allDays = 0
            let now = new Date()
            let global_month = now.getMonth() + 1
            let global_year = now.getFullYear()
            let today = now.getDate()
            $('#rlsj').val(formatDate(now));
            $('.prev').click(function () {
                now.setMonth(now.getMonth() - 1)
                $('#rlsj').val(formatDate(now));
                var d = new Date($('#rlsj').val().substring(0, 4), $('#rlsj').val().substring(5, 7), 1);                //取当年当月中的第一天
                var lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();//获取当月最后一天日期
                if (d.getMonth() < 10) {
                    var mon = "0" + d.getMonth();
                } else {
                    var mon = d.getMonth();
                }
                var kssj = d.getFullYear() + '-' + mon + '-' + "0" + d.getDate();
                var jssj = d.getFullYear() + '-' + mon + '-' + lastDay;
                var ys = $("#doctormc2").attr("data-label");
                if (ys == null || ys == "" || ys == "undefined") {
                    ys = "";
                }
                var ks = $("#ksmz").val();
                if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
                    ks = "";
                }
                //$('#gridList').jqGrid("clearGridData");
                //$('#gridList').jqGrid('setGridParam', {
                //    postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: null, ks: ks },
                //}).trigger('reloadGrid');
                $.najax({
                    url: "/OutPatientManage/OutBook/GetSchedule",
                    dataType: "json",
                    data: { kssj: kssj, jssj: jssj, ys: ys, czztcx: '1', ScheduId: null, ks: ks, lx: $("#mjzbz").val() },
                    type: "POST",
                    success: function (data) {
                        $(".main").show();
                        $(".gridPanel").hide();
                        $("#cxdiv").hide();
                        calendar(data);
                    }
                });
            })
            $('.next').click(function () {
                now.setMonth(now.getMonth() + 1)
                $('#rlsj').val(formatDate(now));
                var d = new Date($('#rlsj').val().substring(0, 4), $('#rlsj').val().substring(5, 7), 1);                //取当年当月中的第一天
                var lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();//获取当月最后一天日期
                if (d.getMonth() < 10) {
                    var mon = "0" + d.getMonth();
                } else {
                    var mon = d.getMonth();
                }
                var kssj = d.getFullYear() + '-' + mon + '-' + "0" + d.getDate();
                var jssj = d.getFullYear() + '-' + mon + '-' + lastDay;
                var ys = $("#doctormc2").attr("data-label");
                if (ys == null || ys == "" || ys == "undefined") {
                    ys = "";
                }
                var ks = $("#ksmz").val();
                if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
                    ks = "";
                }
                //$('#gridList').jqGrid("clearGridData");
                //$('#gridList').jqGrid('setGridParam', {
                //    postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: null, ks: ks },
                //}).trigger('reloadGrid');
                $.najax({
                    url: "/OutPatientManage/OutBook/GetSchedule",
                    dataType: "json",
                    data: { kssj: kssj, jssj: jssj, ys: ys, czztcx: '1', ScheduId: null, ks: ks, lx: $("#mjzbz").val() },
                    type: "POST",
                    success: function (data) {
                        $(".main").show();
                        $(".gridPanel").hide();
                        $("#cxdiv").hide();
                        calendar(data);
                    }
                });

            })
            $('#btn_searchrl').click(function () {
                var sj = $("#rlsj").val();
                now.setMonth(parseInt(sj.substring(5, 7)) - 1)
                now.setFullYear(parseInt(sj.substring(0, 4)))
                var d = new Date($('#rlsj').val().substring(0, 4), $('#rlsj').val().substring(5, 7), 1);                //取当年当月中的第一天
                var lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();//获取当月最后一天日期
                if (d.getMonth() < 10) {
                    var mon = "0" + d.getMonth();
                } else {
                    var mon = d.getMonth();
                }
                var kssj = d.getFullYear() + '-' + mon + '-' + "0" + d.getDate();
                var jssj = d.getFullYear() + '-' + mon + '-' + lastDay;
                var ys = $("#doctormc2").attr("data-label");
                if (ys == null || ys == "" || ys == "undefined") {
                    ys = "";
                }
                var ks = $("#ksmz").val();
                if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
                    ks = "";
                }
                //$('#gridList').jqGrid("clearGridData");
                //$('#gridList').jqGrid('setGridParam', {
                //    postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: null, ks: ks },
                //}).trigger('reloadGrid');
                $.najax({
                    url: "/OutPatientManage/OutBook/GetSchedule",
                    dataType: "json",
                    data: { kssj: kssj, jssj: jssj, ys: ys, czztcx: '1', ScheduId: null, ks: ks, lx: $("#mjzbz").val() },
                    type: "POST",
                    success: function (data) {
                        $(".main").show();
                        $(".gridPanel").hide();
                        $("#cxdiv").hide();
                        calendar(data);
                    }
                });
            });
            $('#rlsj').focus(function () {
                var sj = $("#rlsj").val();
                now.setMonth(parseInt(sj.substring(5, 7)) - 1)
                now.setFullYear(parseInt(sj.substring(0, 4)))
                calendar()
            })
            function calendar(data) {
                $('.content').empty()
                let month = now.getMonth() + 1
                let year = now.getFullYear()

                //得到每个月多少天
                switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        allDays = 31
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        allDays = 30
                        break;
                    default:
                        //判断闰年 得到二月份的天数
                        if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
                            allDays = 29
                        } else {
                            allDays = 28
                        }

                        break;
                }
                //获取当前月份有多少天 生成相应个数的li
                for (let i = 1; i <= allDays; i++) {
                    var shijianstr = '';
                    var pthyzs = 0;
                    var ptsyhys = 0;
                    var zjhyzs = 0;
                    var zjsyhys = 0;
                    var qthyzs = 0;
                    var qtsyhys = 0;
                    if (gridzljyData!=null) {
                        for (var j = 0; j < gridzljyData.length; j++) {
                            if (gridzljyData[j].OutDate.toString().substring(0, 4) == now.getFullYear().toString() && parseInt(gridzljyData[j].OutDate.toString().substring(5, 7)).toString() == (now.getMonth() + 1).toString() && parseInt(gridzljyData[j].OutDate.toString().substring(8, 10)).toString() == i.toString()) {
                                if (gridzljyData[j].RegType == "普通门诊") {
                                    if (gridzljyData[j].TotalNum != 0) {
                                        pthyzs += parseInt(gridzljyData[j].TotalNum);
                                        ptsyhys += (parseInt(gridzljyData[j].TotalNum) - parseInt(gridzljyData[j].YYNum));
                                    }
                                }
                                else if (gridzljyData[j].RegType == "专家门诊") {
                                    if (gridzljyData[j].TotalNum != 0) {
                                        zjhyzs += parseInt(gridzljyData[j].TotalNum);
                                        zjsyhys += (parseInt(gridzljyData[j].TotalNum) - parseInt(gridzljyData[j].YYNum));
                                    }
                                }
                                else {
                                    if (gridzljyData[j].TotalNum != 0) {
                                        qthyzs += parseInt(gridzljyData[j].TotalNum);
                                        qtsyhys += (parseInt(gridzljyData[j].TotalNum) - parseInt(gridzljyData[j].YYNum));
                                    }
                                }
                            }
                        }
                    }
                   else if (data != null && data.rows.length != null) {
                        for (var j = 0; j < data.rows.length; j++) {
                            //alert(data.rows[j].OutDate.toString().substring(0, 4) + "=="+now.getFullYear().toString())
                            //alert(parseInt(data.rows[j].OutDate.toString().substring(5, 7)).toString() + "==" + (now.getMonth() + 1).toString())
                            //alert(parseInt(data.rows[j].OutDate.toString().substring(8, 10)).toString() +"=="+ i.toString());
                            if (data.rows[j].OutDate.toString().substring(0, 4) == now.getFullYear().toString() && parseInt(data.rows[j].OutDate.toString().substring(5, 7)).toString() == (now.getMonth() + 1).toString() && parseInt(data.rows[j].OutDate.toString().substring(8, 10)).toString() == i.toString()) {
                                if (data.rows[j].RegType == "1") {
                                    if (data.rows[j].TotalNum != 0) {
                                        pthyzs += parseInt(data.rows[j].TotalNum);
                                        ptsyhys += (parseInt(data.rows[j].TotalNum) - parseInt(data.rows[j].YYNum));
                                    }
                                }
                                else if (data.rows[j].RegType == "3") {
                                    if (data.rows[j].TotalNum != 0) {
                                        zjhyzs += parseInt(data.rows[j].TotalNum);
                                        zjsyhys += (parseInt(data.rows[j].TotalNum) - parseInt(data.rows[j].YYNum));
                                    }
                                }
                                else {
                                    if (data.rows[j].TotalNum != 0) {
                                        qthyzs += parseInt(data.rows[j].TotalNum);
                                        qtsyhys += (parseInt(data.rows[j].TotalNum) - parseInt(data.rows[j].YYNum));
                                    }
                                }
                            }
                        }
                    }
                    else if (loaddata != null && loaddata.rows.length != null) {
                        for (var j = 0; j < loaddata.rows.length; j++) {
                            //alert(data.rows[j].OutDate.toString().substring(0, 4) + "=="+now.getFullYear().toString())
                            //alert(parseInt(data.rows[j].OutDate.toString().substring(5, 7)).toString() + "==" + (now.getMonth() + 1).toString())
                            //alert(parseInt(data.rows[j].OutDate.toString().substring(8, 10)).toString() +"=="+ i.toString());
                            if (loaddata.rows[j].OutDate.toString().substring(0, 4) == now.getFullYear().toString() && parseInt(loaddata.rows[j].OutDate.toString().substring(5, 7)).toString() == (now.getMonth() + 1).toString() && parseInt(loaddata.rows[j].OutDate.toString().substring(8, 10)).toString() == i.toString()) {
                                if (loaddata.rows[j].RegType == "1") {
                                    if (loaddata.rows[j].TotalNum != 0) {
                                        pthyzs += parseInt(loaddata.rows[j].TotalNum);
                                        ptsyhys += (parseInt(loaddata.rows[j].TotalNum) - parseInt(loaddata.rows[j].YYNum));
                                    }
                                }
                                else if (loaddata.rows[j].RegType == "3") {
                                    if (loaddata.rows[j].TotalNum != 0) {
                                        zjhyzs += parseInt(loaddata.rows[j].TotalNum);
                                        zjsyhys += (parseInt(loaddata.rows[j].TotalNum) - parseInt(loaddata.rows[j].YYNum));
                                    }
                                }
                                else {
                                    if (loaddata.rows[j].TotalNum != 0) {
                                        qthyzs += parseInt(loaddata.rows[j].TotalNum);
                                        qtsyhys += (parseInt(loaddata.rows[j].TotalNum) - parseInt(loaddata.rows[j].YYNum));
                                    }
                                }
                            }
                        }
                    }
                    var topheight = 60;
                    shijianstr = '<div class="rqxs"><span>' + i.toString() + '</span></div>';
                    if (pthyzs != 0) {
                        shijianstr += '<div class="syhys" style="top:' + topheight + 'px"><div  class="progress-bg" style="width:' + ((ptsyhys / pthyzs) * 100) + '%"></div><div class="xszdc" onclick="xzsjlx(\'' + (year + "-" + ((month < 10) ? '0' + month : month) + "-" + ((i < 10) ? '0' + i : i)) + '\',\'pt\')"><span style="padding:2px 5px;">普通</span>余:<span  class="ztsz">' + ptsyhys + '</span></div></div>';
                        topheight -= 46;
                    }
                    if (zjhyzs != 0) {
                        shijianstr += '<div style="top:' + topheight + 'px"><div  class="syhys" style="top:' + topheight + 'px"><div class="progress-zj" style="width:' + ((zjsyhys / zjhyzs) * 100) + '%"></div><div class="xszdc" onclick="xzsjlx(\'' + (year + "-" + ((month < 10) ? '0' + month : month) + "-" + ((i < 10) ? '0' + i : i)) + '\',\'zj\')"><span style="padding:2px 5px;">专家</span>余:<span  class="ztsz">' + zjsyhys + '</span></div></div>';
                        topheight -= 46;
                    }
                    if (qthyzs != 0) {
                        shijianstr += '<div class="syhys" style="top:' + topheight + 'px"><div   class="progress-qt" style="width:' + ((qtsyhys / qthyzs) * 100) + '%"></div><div class="xszdc"  onclick="xzsjlx(\'' + (year + "-" + ((month < 10) ? '0' + month : month) + "-" + ((i < 10) ? '0' + i : i)) + '\',\'qt\')"><span style="padding:2px 5px;">其他</span>余:<span  class="ztsz" >' + qtsyhys + '</span></div></div>';
                    }
                    let li = $('<li />').html(shijianstr)
                    //给对应今天的li添加高亮效果
                    if (i === today && year === global_year && month === global_month) {
                        li.addClass('active')
                    } else { li.addClass('btnli' + i) }
                    $('.content').append(li)
                    if (i === today && year === global_year && month === global_month) {
                        var liactive = $(".active");
                        $(liactive[0]).click(function () {
                            //alert();
                        })
                    }
                    var lis = $(".btnli" + i);
                    $(lis[0]).click(function () {
                        //alert(year + "-" + ((month < 10) ? '0' + month : month) + "-" + ((i < 10) ? '0' + i : i));
                    })

                }
                //思路：每个月一号前面空几个 空的个数为周几就空几个
                now.setDate(1) //将时间设置成本月的一号
                let firstDay = now.getDay() // 获取一号是周几
                for (let i = 0; i < firstDay; i++) { // 循环次数为对应的是周几
                    now.setDate(now.getDate() - 1) //每循环一次 日期往前倒一天
                    let li = $('<li />').text(now.getDate()).addClass('disabled') //生成相对应的空格
                    $('.content').prepend(li)
                }
                now.setDate(now.getDate() + firstDay) //将时间设置回当前时间 否则影响后面时间的获取
                now.setDate(allDays)
                //思路：每个月最后一天后面空几个 空的个数为6减周几就空几个
                now.setDate(allDays) //将时间设置成每月最后一天
                let lastDay = 6 - now.getDay() //最后空的个数
                for (let i = 0; i < lastDay; i++) {
                    now.setDate(now.getDate() + 1) //每循环一次 日期往后加一天
                    let li = $('<li />').text(now.getDate()).addClass('disabled') //生成相对应的空格
                    $('.content').append(li)
                }
                now.setDate(now.getDate() - lastDay) //将时间设置回当前时间 否则影响后面时间的获取
                now.setDate(1)

                $('.nowTime').html(now.getFullYear() + '年' + (now.getMonth() + 1) + '月')
            }

            calendar()
        }
        function xzsjlx(sj, lx) {
            $.modalOpen({
                id: "RosterCalendarForm",
                title: "排班内容",
                url: "/OutPatientManage/OutBook/RosterCalendarForm?sj=" + sj + "&lx=" + lx,
                width: "950px",
                height: "600px",
                callBack: function (iframeId) {
                    $.currentWindow(iframeId).AcceptClick(function (ScheduId) {
                        $(".main").hide();
                        $(".gridPanel").show();
                        $("#cxdiv").show();
                        isf = true;
                        getGriddata(ScheduId);
                    });
                }
            });
        }
        function zhrllist() {
            //获取所有行Id，遍历使编辑框处于保存状态

            var rowIds = $("#gridList").jqGrid('getDataIDs');
            for (var i = 0; i < rowIds.length; i++) {
                var saveResult = $("#gridList").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);
                if (!saveResult) {
                    EnableInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                    return;   //保存失败，则return
                }
            }
            var gridzljyData = $("#gridList").jqGrid('getRowData');
            gridzljyData.push($("#gridList").jqGrid('getRowData', rowIds[rowIds.length - 1]));
            if (!gridzljyData || gridzljyData.length == 0) {
                EnableRehabInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                $.modalAlert("请选择需要修改的排班信息", 'warning');
                return;
            }
            $.each(gridzljyData, function () {    //去掉action
                for (var i = 0; i < $(this).length; i++) {
                    delete $(this)[i].action;
                }
            });
            $(".main").show();
            $(".gridPanel").hide();
            $("#cxdiv").hide();
            loadrl(gridzljyData,null);
        }
        $('#btn_zhrl').click(function () {
            isf = false;
            var po = new Date($('#kssj').val().substring(0, 4), $('#kssj').val().substring(5, 7), 1);
            var d = new Date();                //取当年当月中的第一天
            var lastDay = new Date(po.getTime() - 1000 * 60 * 60 * 24).getDate();//获取当月最后一天日期
            if (d.getMonth() < 9){
                var mon = "0" + (d.getMonth()+1);
            } else {
                var mon = (d.getMonth() + 1);
            }
            if (d.getDate() < 10) {
                var day = "0" + d.getDate();
            } else {
                var day = d.getDate();
            }

            //var kssj = d.getFullYear() + '-' + mon + '-' + day;
            var kssj = d.getFullYear() + '-' + mon + '-' + '01';//2022年8月19日  改为显示当月日历
            var jssj = d.getFullYear() + '-' + mon + '-' + lastDay;
            var ys = $("#doctormc2").attr("data-label");
            if (ys == null || ys == "" || ys == "undefined") {
                ys = "";
            }
            var ks = $("#ksmz").val();
            if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
                ks = "";
            }
            //$('#gridList').jqGrid("clearGridData");
            //$('#gridList').jqGrid('setGridParam', {
            //    postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: null, ks: ks },
            //}).trigger('reloadGrid');
            $.najax({
                url: "/OutPatientManage/OutBook/GetSchedule",
                dataType: "json",
                data: { kssj: kssj, jssj: jssj, ys: ys, czztcx: '1', ScheduId: null, ks: ks, lx: $("#mjzbz").val() },
                type: "POST",
                success: function (data) {
                    $(".main").show();
                    $(".gridPanel").hide();
                    $("#cxdiv").hide();
                    loadrl(null, data);
                }
            });
        });
        function btn_details(nowdate) {
            $(".main").hide();
            $(".gridPanel").show();
            $("#cxdiv").show();
            $("#kssj").val(nowdate);
            $("#jssj").val(nowdate);
            getGriddata(null);
        }
        function newtouch_event_f4() {
            isf = true;
            $(".main").hide();
            $(".gridPanel").show();
            $("#cxdiv").show();
            getGriddata(null);

        };
        $(function () {
            GridInit();
            $(".main").show();
            $(".gridPanel").hide();
            $("#cxdiv").hide();
            isf = false;
            var d = new Date($('#rlsj').val().substring(0, 4), $('#rlsj').val().substring(5, 7), 1);                //取当年当月中的第一天
            var lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();//获取当月最后一天日期
            if (d.getMonth() < 10) {
                var mon = "0" + d.getMonth();
            } else {
                var mon = d.getMonth();
            }
            var kssj = d.getFullYear() + '-' + mon + '-' + "0" + d.getDate();
            var jssj = d.getFullYear() + '-' + mon + '-' + lastDay;
            var ys = $("#doctormc2").attr("data-label");
            if (ys == null || ys == "" || ys == "undefined") {
                ys = "";
            }
            var ks = $("#ksmz").val();
            if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
                ks = "";
            }
            //$('#gridList').jqGrid("clearGridData");
            //$('#gridList').jqGrid('setGridParam', {
            //    postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: null, ks: ks },
            //}).trigger('reloadGrid');
            $.najax({
                url: "/OutPatientManage/OutBook/GetSchedule",
                dataType: "json",
                data: { kssj: kssj, jssj: jssj, ys: ys, czztcx: '1', ScheduId: null, ks: ks, lx: $("#mjzbz").val() },
                type: "POST",
                success: function (data) {
                    $(".main").show();
                    $(".gridPanel").hide();
                    $("#cxdiv").hide();
                    loadrl(null,data);
                }
            });
        });
        function newtouch_event_f8() {
            if (isf == false) {
                $.modalAlert("请选择需要修改的排班信息", 'warning');
                return;
            }
            //获取所有行Id，遍历使编辑框处于保存状态
            var rowIds = $("#gridList").jqGrid('getDataIDs');
            if (rowIds.length == 0) {
                $.modalAlert("请选择需要修改的排班信息", 'warning');
                return;
            }
            for (var i = 0; i < rowIds.length; i++) {
                var saveResult = $("#gridList").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);

                if (!saveResult) {
                    EnableInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                    return;   //保存失败，则return
                }
            }
            var gridzljyData = $("#gridList").jqGrid('getRowData');
            gridzljyData.push($("#gridList").jqGrid('getRowData', rowIds[rowIds.length - 1]));
            if (!gridzljyData || gridzljyData.length == 0) {
                EnableRehabInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                $.modalAlert("请选择需要修改的排班信息", 'warning');
                return;
            }
            $.each(gridzljyData, function () {    //去掉action
                for (var i = 0; i < $(this).length; i++) {
                    delete $(this)[i].action;
                }
            });
            $.najax({
                url: "/OutPatientManage/OutBook/SaveDataPbhy",
                dataType: "json",
                data: { pbList: gridzljyData },
                type: "POST",
                success: function (data) {
                    getGriddata();
                    $.modalAlert("保存成功", 'warning');
                },
                complete: function () {
                    EnableRehabInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                }
            });
        }
        function EnableRehabInlineEditBox() {
            var rowIds = $("#gridList").jqGrid('getDataIDs');
            for (var i = 0; i < rowIds.length; i++) {
                $("#gridList").jqGrid('editRow', rowIds[i], true, null, null, null, null, function (callbackRowId) {     //true: when set to true we can use [Enter] key to save the row and [Esc] to cancel editing.

                });
            }
        }
        ///医生
        $("#doctormc").newtouchFloatingSelector({
            height: 200,
            width: 480,
            clickautotrigger: true,
            filter: function (keyword) {
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();

                $.each(top.window.clients.sysStaffDutyList, function (idx, val) {
                    if (((val.StaffPY && val.StaffPY.toLowerCase().indexOf(keyword.toLowerCase()) >= 0)
                        || (val.StaffName && val.StaffName.indexOf(keyword.toLowerCase()) >= 0)
                        || keyword.trim() == "")
                        && val.DutyCode == "Doctor") {
                        resultObjArr.push(val);
                    }
                });
                return resultObjArr;

            },
            caption: "医生",
            colModel: [
                {
                    label: '工号', name: 'StaffGh', widthratio: 25
                },
                {
                    label: '名称', name: 'StaffName', widthratio: 25
                },
                { label: '拼音', name: 'StaffPY', widthratio: 25 }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#doctormc").attr("data-label", $thistr.find("td:eq(0)").html());
                $("#doctormc").val($thistr.find('td:eq(1)').html());
                return;
            }
        });
        //科室
        $('#ksmz').newtouchFloatingSelector({
            width: 400,
            height: 180,
            caption: "科室",
            clickautotrigger: true,
           /*
            url: '/OutpatientManage/OutpatientRegSchedule/GetSysDepartmentList',
            ajaxmethod: 'POST',
            ajaxreqdata: function () {
                var reqData = {};
                reqData.keyword = $("#ksmz").val();
                return reqData;
            },
            */
            filter: function (keyword) {
                var resultObjArr = new Array();
                $.each(top.window.clients.sysDepartList, function (idx, val) {
                    if (keyword == "" || val.Name.indexOf(keyword) >= 0) {
                        if (val.zlks == "1" || val.zlks == undefined || val.zlks == null) {
                            resultObjArr.push(val);
                        }
                    }
                });
                return resultObjArr;
            },
            itemdbclickhandler: function ($thistr) {
                $('#ksmz').attr('data-ks', $thistr.attr('data-code'))
                    .val($thistr.attr('data-Name'));
                $('#ks').attr('data-ks', $thistr.attr('data-code'))
                    .val($thistr.attr('data-code'));
                ////根据科室编号获取医生姓名
                //var ks = $thistr.attr('data-code');

                //$.ajax({
                //    url: "/OutPatientManage/OutBook/getStaffByKs?ks=" + ks,
                //    dataType: "json",
                //    async: false,
                //    success: function (data) {
                //        var staff = "";
                //        for (i = 0; i < data.length; i++) {
                //            staff += data[i] + ",";
                //        }
                //        if (staff.indexOf(",") != -1) {
                //            staff = staff.substring(0, staff.length - 1);
                //        }
                //        $("#ysxm").val(staff);
                //    }
                //});
            },
            colModel: [{ label: '代码', name: 'Code', widthratio: 30 },
            { label: '科室名称', name: 'Name', widthratio: 30 },
            { label: '拼音', name: 'py', widthratio: 30 }
            ]
        });
        ///医生
        $("#doctormc2").newtouchFloatingSelector({
            height: 200,
            width: 480,
            clickautotrigger: true,
            filter: function (keyword) {
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();

                $.each(top.window.clients.sysStaffDutyList, function (idx, val) {
                    if (((val.StaffPY && val.StaffPY.toLowerCase().indexOf(keyword.toLowerCase()) >= 0)
                        || (val.StaffName && val.StaffName.indexOf(keyword.toLowerCase()) >= 0)
                        || keyword.trim() == "")
                        && val.DutyCode == "Doctor") {
                        resultObjArr.push(val);
                    }
                });
                return resultObjArr;

            },
            caption: "医生",
            colModel: [
                {
                    label: '工号', name: 'StaffGh', widthratio: 25
                },
                {
                    label: '名称', name: 'StaffName', widthratio: 25
                },
                { label: '拼音', name: 'StaffPY', widthratio: 25 }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#doctormc2").attr("data-label", $thistr.find("td:eq(0)").html());
                $("#doctormc2").val($thistr.find('td:eq(1)').html());
                return;
            }
        });
        //查询
        $('#btn_search').click(function () {
            getGriddata(null);
        });
        function btn_searchrl() {
            var sj = $("#rlsj").val();
            now.setMonth(parseInt(sj.substring(5, 7)) - 1)
            now.setFullYear(parseInt(sj.substring(0, 4)))
            var d = new Date($('#rlsj').val().substring(0, 4), $('#rlsj').val().substring(5, 7), 1);                //取当年当月中的第一天
            var lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();//获取当月最后一天日期
            if (d.getMonth() < 10) {
                var mon = "0" + d.getMonth();
            } else {
                var mon = d.getMonth();
            }
            var kssj = d.getFullYear() + '-' + mon + '-' + "0" + d.getDate();
            var jssj = d.getFullYear() + '-' + mon + '-' + lastDay;
            var ys = $("#doctormc2").attr("data-label");
            if (ys == null || ys == "" || ys == "undefined") {
                ys = "";
            }
            var ks = $("#ksmz").val();
            if (ks == null || ks == "" || ks == "undefined" || $("#ksmz").val() == "") {
                ks = "";
            }
            //$('#gridList').jqGrid("clearGridData");
            //$('#gridList').jqGrid('setGridParam', {
            //    postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: null, ks: ks },
            //}).trigger('reloadGrid');
            $.najax({
                url: "/OutPatientManage/OutBook/GetSchedule",
                dataType: "json",
                data: { kssj: kssj, jssj: jssj, ys: ys, czztcx: '1', ScheduId: null, ks: ks, lx: $("#mjzbz").val() },
                type: "POST",
                success: function (data) {
                    $(".main").show();
                    $(".gridPanel").hide();
                    $("#cxdiv").hide();
                    calendar(data);
                }
            });
        }
        function getGriddata(ScheduId) {
            if ($('#kssj').val() && $('#jssj').val() && ($('#kssj').val() > $('#jssj').val())) {
                $.modalAlert("开始日期不能大于结束日期", 'warning');
                return;
            }
            if (ScheduId != null && ScheduId!="") {
                $("#czztcx").val('0');
            }
            var kssj = $('#kssj').val();
            var jssj = $('#jssj').val();
            var ys = $("#doctormc").attr("data-label");
            if (ys == null || ys == "" || ys == "undefined") {
                ys = "";
            }
            $('#gridList').jqGrid("clearGridData");
            $('#gridList').jqGrid('setGridParam', {
                postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val(), ScheduId: ScheduId },
            }).trigger('reloadGrid');
        }
        function GridInit() {
            if ($('#kssj').val() && $('#jssj').val() && ($('#kssj').val() > $('#jssj').val())) {
                $.modalAlert("开始日期不能大于结束日期", 'warning');
                return;
            }
            var kssj = $('#kssj').val();
            var jssj = $('#jssj').val();
            var ys = $("#doctormc").attr("data-label");
            if (ys == null || ys == "" || ys == "undefined") {
                ys = "";
            }
            var $gridList = $("#gridList");
            $gridList.dataGrid({
                url: "/OutPatientManage/OutBook/GetSchedule",
                autowidth: true,
                postData: { kssj: kssj, jssj: jssj, ys: ys, czztcx: $("#czztcx").val() },
                unwritten: false,
                height: $(window).height() - 180,
                colModel: [
                    { label: "ghpbId", name: "ScheduId", hidden: true },
                    { label: "编号", name: "ghpbId", width: 40, align: 'left', hidden: true },
                    {
                        label: "出诊日期", name: "OutDate", width: 120, align: 'left', formatter: function (val) {
                            if (val.indexOf("T")) {
                                return val.replace('T', ' ');
                            } else {
                                return val;
                            }
                        }
                    },
                    {
                        label: "星期", name: "weekdd", width: 80, align: 'left', formatter: function (val) {
                            if (val == "1") {
                                return "星期一";
                            }
                            else if (val == "2") {
                                return "星期二";
                            }
                            else if (val == "3") {
                                return "星期三";
                            }
                            else if (val == "4") {
                                return "星期四";
                            }
                            else if (val == "5") {
                                return "星期五";
                            }
                            else if (val == "6") {
                                return "星期六";
                            }
                            else if (val == "7") {
                                return "星期日";
                            }
                        }
                    },
                    { label: "医生工号", name: "ysgh", width: 50, align: 'left', hidden: true },
                    { label: "医生姓名", name: "ysxm", width: 50, align: 'left' },
                    { label: "出诊科室", name: "czks", width: 80, align: 'left', hidden: true },
                    { label: "出诊科室名称", name: "czksmc", width: 80, align: 'left' },
                    {
                        label: "挂号类型", name: "RegType", width: 60, align: 'left', formatter: function (val) {
                            if (val == "1") {
                                return "普通门诊";
                            }
                            else if (val == "2") {
                                return "急诊";
                            } else if (val == "3") {
                                return "专家门诊";
                            }
                            else if (val == "4") {
                                return "特病门诊";
                            }
                            else if (val == "5") {
                                return "重大疾病门诊";
                            }
                            else if (val == "6") {
                                return "慢性病门诊";
                            }
                            else if (val == "7") {
                                return "居民两病";
                            }
                            else if (val == "8") {
                                return "意外伤害门诊";
                            }
                            else if (val == "9") {
                                return "生育门诊";
                            }
                            else if (val == "10") {
                                return "耐多药结核门诊";
                            }
                            else if (val == "11") {
                                return "儿童两病门诊";
                            }
                        }
                    },
                    { label: "职称", name: "Title", width: 90, align: 'left' },
                    { label: "出诊时间段", name: "PeriodDesc", width: 80, align: 'left' },
                    { label: "yynum", name: "YYNum", width: 80, align: 'left', hidden: true },
                    { label: "号源", name: "TotalNum", width: 60, align: 'left', editable: true, editrules: { numeric: true, minValue: 0 } },
                    {
                        label: "剩余号源数", name: "action", width: 80, align: 'left', formatter: function (cellvalue, options, rowobject) {
                            return (rowobject.TotalNum - rowobject.YYNum)
                        }
                    },
                    { label: "开始时间", name: "PeriodStart", width: 40, align: 'left', hidden: true },
                    { label: "结束时间", name: "PeriodEnd", width: 40, align: 'left', hidden: true },
                    { label: "挂号费", name: "RegFee", width: 60, align: 'left' },
                    {
                        label: "是否开放网络预约挂号", name: "IsBook", width: 80, align: 'left', formatter: function (val) {
                            if (val == "1") {
                                return "是";
                            }
                            else if (val == "0") {
                                return "否";
                            }
                        }
                    },
                    { label: "挂号项目", name: "sfxmmc", width: 80, align: 'left' },
                    {
                        label: "操作", name: "action", width: 80, align: 'left',
                        formatter: function (cellvalue, options, rowobject) {
                            var str = "";
                            if (rowobject.istz == "1") {
                                str += '&nbsp;<a class="btn btn-default" title="出诊" style="width: 50%;background-color:#ff0000" onclick="istz(\'' + rowobject.ScheduId + '\',\'' + '0' + '\')">出诊</a>';
                            } else {
                                str += '&nbsp;<a class="btn btn-default" title="停诊" style="width: 50%;background-color:#73d4ff" onclick="istz(\'' + rowobject.ScheduId + '\',\'' + '1' + '\')">停诊</a>';
                            }
                            return str;
                        }
                    },
                    { label: "停诊原因", name: "tzyy", width: 150, align: 'left', editable: true, editrules: { numeric: false, minValue: 0 } },
                    { label: "附加项目", name: "zlxmmc", width: 80, align: 'left' },
                    { label: "zt", name: "zt", width: 50, align: 'left', hidden: true },
                ],
                onSelectRow: function (rowid) {
                },
                gridComplete: function () {
                    //启用行内编辑框
                    EnableInlineEditBox();
                }
            });
        }
        //启用行内编辑框
        function EnableInlineEditBox() {
            //Grid加载完成开启行内编辑   1.先获取所有行Id  2.遍历调用editRow方法
            var rowIds = $("#gridList").jqGrid('getDataIDs');
            for (var i = 0; i < rowIds.length; i++) {
                $("#gridList").jqGrid('editRow', rowIds[i], true, null, null, null, null, function (callbackRowId) {     //true: when set to true we can use [Enter] key to save the row and [Esc] to cancel editing.

                });

            }
        }

        function istz(ScheduId, czzt) {
            var tzyystr = "";
            if (czzt == "1") {
                //获取所有行Id，遍历使编辑框处于保存状态
                var rowIds = $("#gridList").jqGrid('getDataIDs');
                for (var i = 0; i < rowIds.length; i++) {
                    var saveResult = $("#gridList").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);
                    if (!saveResult) {
                        EnableInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                        return;   //保存失败，则return
                    }
                }
                var gridzljyData = $("#gridList").jqGrid('getRowData');
                gridzljyData.push($("#gridList").jqGrid('getRowData', rowIds[rowIds.length - 1]));
                if (!gridzljyData || gridzljyData.length == 0) {
                    EnableRehabInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                    $.modalAlert("请选择需要修改的排班信息", 'warning');
                    return;
                }
                $.each(gridzljyData, function () {    //去掉action
                    for (var i = 0; i < $(this).length; i++) {
                        delete $(this)[i].action;
                    }
                });
                for (var i = 0; i < gridzljyData.length; i++) {
                    if (gridzljyData[i].ScheduId == ScheduId) {
                        if (gridzljyData[i].tzyy == "") {
                            $.modalAlert("请填写停诊原因！", 'warning');
                            EnableInlineEditBox();
                            return;
                        } else {
                            tzyystr = gridzljyData[i].tzyy;
                        }

                    }
                }
            }
            $.najax({
                url: "/OutPatientManage/OutBook/SaveDatatzcz",
                dataType: "json",
                data: { ScheduId: ScheduId, czzt: czzt, tzyy: tzyystr },
                type: "POST",
                success: function (data) {
                    $.modalAlert("保存成功", 'warning');
                    EnableInlineEditBox();
                    getGriddata(null);
                }
            });
        }
        function btn_set() {
            var ghpbId = $("#gridList").jqGridRowValue().ghpbId;
            var OrganizeId = $("#gridList").jqGridRowValue().OrganizeId;
            if (!!!ghpbId) {
                $.modalAlert("请选中一条信息", 'warning');
                return false;
            }
            $.modalOpen({
                id: "updateForm",
                title: "编辑排班",
                url: "/OutPatientManage/OutBook/Form?ghpbId=" + ghpbId + "&orgId=" + OrganizeId + "&type=update",
                width: "800px",
                height: "900px",
                callBack: function (iframeId) {
                    $.currentWindow(iframeId).AcceptClick(function () {
                        $("#gridList").resetSelection();
                        $("#gridList").trigger("reloadGrid");
                    });
                }
            });
        }

        //新增排班
        function btn_add() {
            var ghpbId = 0;
            var OrganizeId = $("#gridList").jqGridRowValue().OrganizeId;
            $.modalOpen({
                id: "updateForm",
                title: "编辑排班",
                url: "/OutPatientManage/OutBook/Form?ghpbId=" + ghpbId + "&orgId=" + OrganizeId + "&type=add",
                width: "800px",
                height: "900px",
                callBack: function (iframeId) {
                    $.currentWindow(iframeId).AcceptClick(function () {
                        $("#gridList").resetSelection();
                        $("#gridList").trigger("reloadGrid");
                    });
                }
            });
        }

        //生成排班
        function btn_create() {
            $.modalOpen({
                id: "updateForm",
                title: "生成排班",
                url: "/OutPatientManage/OutBook/Create",
                width: "800px",
                height: "800px",
                btn: null,
                callBack: function (iframeId) {
                    $.currentWindow(iframeId).AcceptClick(function () {
                        $("#gridList").resetSelection();
                        $("#gridList").trigger("reloadGrid");
                    });
                }
            });
        }
        //生成排班时间
        function btn_createtime() {
            $.modalOpen({
                id: "TimeForm",
                title: "编辑排班时间",
                url: "/OutPatientManage/OutBook/TimeForm?type=add",
                width: "600px",
                height: "600px",
                callBack: function (iframeId) {
                    $.currentWindow(iframeId).AcceptClick(function () {
                    });
                }
            });
        }
    </script>
